package com.oren.activity.ctrl;

import com.oren.activity.pojo.ActDeployment;
import com.oren.activity.service.ActDeploymentService;
import com.oren.base.ctrl.BaseController;
import com.oren.base.pojo.Page;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: wangshiwei
 * Date: 2018/7/31
 * Time: 14:43
 * describe:
 */
@Controller
@RequestMapping("/actDeployment")
public class ActDeploymentController extends BaseController{

    @Autowired
    private ActDeploymentService deploymentService;

    @RequestMapping("/pageList")
    @ResponseBody
    public Page<ActDeployment> getPageList(ActDeployment actDeployment,int page,int rows){
        return deploymentService.pageList(actDeployment,page,rows);
    }

    @RequestMapping("/fileLoad")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request,MultipartFile file,String fileName)throws Exception{
        //用下面这个方法需要屏蔽掉springmvc的自带上传的功能
        /*String lkll=request.getParameter("fileName");
        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        List fileList=upload.parseRequest(request);
        for (Object item:fileList) {
            FileItem fileItem=(FileItem)item;
            if(!fileItem.isFormField()){ //判断是否是多功能表单提交
                String fileName=fileItem.getName();
               // logger.debug("上传的文件名为："+fileName);
                int fileLengh=fileName.split("\\\\").length;
                fileName=fileName.split("\\\\")[fileLengh-1];
               // logger.debug("处理后的文件名为："+fileName);
                //获取文件流 解析excel
                InputStream fileInputStream=fileItem.getInputStream();
            }else{

            }
        }*/

        //部署上传的zip文件
        InputStream inputStream=file.getInputStream();
        ZipInputStream zipInputStream=new ZipInputStream(inputStream);
        Deployment deployment=processEngine.getRepositoryService().
                createDeployment().
                addZipInputStream(zipInputStream).deploy();

        //获取部署的流程定义类
        ProcessDefinition pd =processEngine.getRepositoryService().createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();

        //添加相关数据到数据库中
        ActDeployment actDeployment=new ActDeployment();
        actDeployment.setCreateTime(new Date());
        actDeployment.setCreateUser(getSessionUserName(request));
        actDeployment.setDepId(pd.getId());
        actDeployment.setDepKey(pd.getKey());
        actDeployment.setDeploymentId(pd.getDeploymentId());
        actDeployment.setVersion(pd.getVersion());
        actDeployment.setDeploymentName(fileName);
        deploymentService.deploymentCreate(actDeployment);
        return successMessage("部署成功");
    }

    @RequestMapping("/findAllDeploy")
    @ResponseBody
    public  List<Map<String, Object>> findAllDeploy(){
        return deploymentService.findAllDeploy();
    }


}
